<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Jobs
 *
 * @author ZzNinjakyozZ
 */
class Manage_Jobs extends CI_Controller{
    var $Array_Data;
    var $CI;
    
    public function __construct() {
        parent::__construct();
        $this->CI =& get_instance();
        
        if(update_user() != 3){
            redirect(site_url() . '/welcome/not_authorized');
        };   
        $this->Array_Data['UserInfo'] = $this->session->userdata('UserInfo');
    }
    
    public function Index()
    {
        $this->CI->load->model('Post_Applies_model','PA');
        $this->CI->load->model('Favorite_Posts_model','FP');
        $this->CI->load->model('Posts_model','Posts');
        if(!isset($_GET['section']) || $_GET['section'] == 'applies')
        {
            /*----- Page Caculate ----*/
            if(isset($_GET['Page']))
                $Current_Page = $_GET['Page'];
            else
                $Current_Page = 1;
            $offset = ($Current_Page-1) * 10;

            $this->Array_Data['TotalNum']       = $this->CI->PA->Get_Num(
                    array('RelUserEmailGroup3' => $this->Array_Data['UserInfo']['UserEmail'])); 
            $this->Array_Data['Current_Page']   = $Current_Page;
            //Caculate Limit
            $limit = $this->Array_Data['TotalNum'] - (($Current_Page-1)*10);
            $limit = ($limit>=10)?10:$limit;
            /*---- End of Page Caculate ----*/        
            $PAs = $this->CI->PA->Get_Advanced('*',array('RelUserEmailGroup3' => $this->Array_Data['UserInfo']['UserEmail']),
                    "ApplyDate desc",$limit, $offset);
            $temp = array();
            foreach($PAs as $PA){
                $Post       = $this->CI->Posts->Get(array('PostID' => $PA['RelPostID']));
                $Post       = $Post[0];
                $Post['IsFavorite'] = $this->CI->FP->Check(array(
                    'RelPostID'     => $Post['PostID'],
                    'RelUserEmail'  => $this->Array_Data['UserInfo']['UserEmail']
                )); 
                $temp[] = $Post;
            }
            $this->Array_Data['Posts'] = $temp;
            $this->load->view('user_group3/manage_posts/apply_posts_view',$this->Array_Data);
        }
        else if($_GET['section'] == 'favorite_posts'){
            /*----- Page Caculate ----*/
            if(isset($_GET['Page']))
                $Current_Page = $_GET['Page'];
            else
                $Current_Page = 1;
            $offset = ($Current_Page-1) * 10;

            $this->Array_Data['TotalNum']       = $this->CI->FP->Get_Num(
                    array('RelUserEmail' => $this->Array_Data['UserInfo']['UserEmail'])); 
            $this->Array_Data['Current_Page']   = $Current_Page;
            //Caculate Limit
            $limit = $this->Array_Data['TotalNum'] - (($Current_Page-1)*10);
            $limit = ($limit>=10)?10:$limit;
            /*---- End of Page Caculate ----*/
            $FPs = $this->CI->FP->Get_Advanced('*',array('RelUserEmail' => $this->Array_Data['UserInfo']['UserEmail']),
                        "AddPostTime desc",$limit, $offset);
            $temp = array();
            foreach($FPs as $FP){
                $Post       = $this->CI->Posts->Get(array('PostID' => $FP['RelPostID']));
                $Post       = $Post[0];
                $Post['IsFavorite'] = $this->CI->FP->Check(array(
                    'RelPostID'     => $Post['PostID'],
                    'RelUserEmail'  => $this->Array_Data['UserInfo']['UserEmail']
                )); 
                $temp[] = $Post;
            }
            $this->Array_Data['Posts'] = $temp;
            $this->load->view('user_group3/manage_posts/favorite_posts_view',$this->Array_Data);
            
        }
    }
}